﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.XPath;
using System.Xml;
using Microsoft.Office.InfoPath;
using System.IO;

public partial class SigForm : System.Web.UI.Page
{
    protected string SigXPath;
    protected string XsnLocation;
    protected string XmlLocation;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            XsnLocation = Request.QueryString["XsnLocation"];
            XmlFormView1.XsnLocation = XsnLocation;
        }
        SigXPath = Request.QueryString["SigXPath"];
        try
        {
            XmlLocation = Request.QueryString["XmlLocation"];
            XmlFormView1.XmlLocation = XmlLocation;
        }
        catch { }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ///my:WorkOrderRequest/my:Footer/my:Signature
        XmlFormView1.DataBind();
        XPathNavigator xNavMain =
      XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
        ///my:WorkOrderRequest/my:Header/my:NameOfProject
        XmlNamespaceManager xNameSpace = 
        new XmlNamespaceManager(new NameTable());
        xNameSpace.AddNamespace("my", XmlFormView1.XmlForm.NamespaceManager.
           LookupNamespace("my").ToString());
        XPathNavigator fTextBox1 = xNavMain.SelectSingleNode(
       SigXPath, xNameSpace);
        if (fTextBox1.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
        { fTextBox1.DeleteSelf(); }
        fTextBox1.SetValue(TextBox1.Text.Split(',')[1]);
        Panel1.Visible = false;
        XmlFormView1.Visible = true;

    }
    protected void InitForm(object sender, EventArgs e)
    {
        
    }
    protected void XmlFormView1_NotifyHost(object sender, Microsoft.Office.InfoPath.Server.Controls.NotifyHostEventArgs e)
    {
        TextBox1.Text = e.Notification.ToString();
        if (e.Notification.ToString() == "sig")
        {
            Panel1.Visible = true;
            XmlFormView1.Visible = false;
        }
    }
    public System.Drawing.Image Base64ToImage(string base64String)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(imageBytes, 0,
          imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
        return image;
    }
    
}